package com.lianwei.hospital.Service.Impl;

import com.lianwei.hospital.Service.UserService;
import com.lianwei.hospital.mapper.UserInfoMapper;
import com.lianwei.hospital.module.entity.DoctorInfo;
import com.lianwei.hospital.module.entity.UserInfo;
import com.lianwei.hospital.module.vo.DoctorVO;
import com.lianwei.hospital.module.vo.UserVO;
import com.lianwei.hospital.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service("UserService")
public class UserInfoImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo selectByPrimaryKey(String id) throws Exception {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insertUser(UserInfo userInfo) throws Exception {
        return userInfoMapper.insert(userInfo)>0;
    }

    @Override
    public List<UserInfo> selectAll() throws Exception {
        return userInfoMapper.selectAll();
    }

    @Override
    public boolean deleteById(String id) throws Exception {
        return userInfoMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public boolean updateById(UserInfo userInfo) throws Exception {
        return userInfoMapper.updateByPrimaryKey(userInfo)>0;
    }

    @Override
    public PageUtil<UserInfo> findPage(UserVO query) throws Exception {
        /*
         * 1.获取总记录数
         * 2.如果总记录数 > 0 查询数据
         *  将数据封装到PageUtil
         * 否则，返回null
         */
        int count=userInfoMapper.countBySelectActive(query);
        if (count>0){
            List<UserInfo> data=userInfoMapper.selectListBySelectActive(query);
            return new PageUtil<>(query.getCurrentPage(), query.getPageSize(),count,data);
        }
        return null;
    }

    @Override
    public UserInfo login(String id, String password) throws Exception {
        UserInfo userInfo= userInfoMapper.findByUsername(id);
        if (userInfo == null){
            throw new Exception("用户未注册");
        }else {
            if (userInfo.getUPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
                return userInfo;
            }else {
                throw new Exception("用户名密码错误");
            }
        }
    }
}
